layui.use(['form', 'layer', 'jquery'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer
    $ = layui.jquery;

    function getQueryString(name) {
              var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
              var r = window.location.search.substr(1).match(reg);

              if (r != null) {
                 return unescape(r[2]);
              }
              return null;
      }

    $(function(){
        if (getQueryString("sessionTimeout")) {
            // layer.msg("登录超时",{icon: 5})
            if (self != top)
                top.location.href = "/login?sessionTimeout=true&JSessionId"+getQueryString("JSessionId")
        }
    })


    $(".loginBody .seraph").click(function () {
        layer.msg("这只是做个样式，至于功能，你见过哪个后台能这样登录的？还是老老实实的找管理员去注册吧", {
            time: 5000
        });
    })


    //登录按钮
    form.on("submit(login)", function (data) {
        var that = $(this)
        $(this).text("登录中...").attr("disabled", "disabled").addClass("layui-disabled");
        setTimeout(function () {
            // window.location.href = "/layuicms2.0";
            var encryptor = new JSEncrypt()  // 创建加密对象实例
            //之前ssl生成的公钥，复制的时候要小心不要有空格
            var pubKey = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDFdHKx/OoRejVEsM/a1grgXMENZmQLlBV/WZm8h62rAbvbytUjg/A9iV06SQgiRdN23s6Eeuq+5Sr1TsjKoWIvkQoeSD5nBkZPIy+Sn/IFHRxowsz0VMd8zXRYKyaSjbgTSgwCp4pxsESGRalB1FL13ghd8WkhOaI9wQDgR2hsLwIDAQAB';
            encryptor.setPublicKey(pubKey)//设置公钥
            data.field.password = encryptor.encrypt(data.field.password);  // 对内容进行加密
            $.post('/auth/login', data.field, function (res) {
                if (res.code == 0) {
                    top.location.href = "/index"
                } else {
                    layer.msg(res.msg);
                }
                that.text("登录").removeAttr("disabled").removeClass("layui-disabled");
            });
        }, 100);
        return false;
    });

    //表单输入效果
    $(".loginBody .input-item").click(function (e) {
        e.stopPropagation();
        $(this).addClass("layui-input-focus").find(".layui-input").focus();
    })
    $(".loginBody .layui-form-item .layui-input").focus(function () {
        $(this).parent().addClass("layui-input-focus");
    })
    $(".loginBody .layui-form-item .layui-input").blur(function () {
        $(this).parent().removeClass("layui-input-focus");
        if ($(this).val() != '') {
            $(this).parent().addClass("layui-input-active");
        } else {
            $(this).parent().removeClass("layui-input-active");
        }
    })
})
